using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Data;
using Common;
using Model.Enum;

namespace BLL.Search
{
    public class CommonSearch 
    {
        public CommonSearch() { }

        /// <summary>
        /// DataTable
        /// matchmodes(Equal, NotEqual, In, NotIn, Like, NotLike, MoreThan, MoreThanEqual, LessThan, LessThanEqual, StartWith, NotStartWith, Equal)
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="fields"></param>
        /// <param name="matchmodes"></param>
        /// <param name="fieldsvalue"></param>
        /// <param name="sortFields"></param>
        /// <param name="sortMode"></param>
        /// <returns></returns>
        public static DataTable ExecSelect(string tableName, string[] fields, string[] matchmodes, string[] fieldsvalue, string[] sortFields, string[] sortMode)
        {
            SQLServerDAL.Search.SearchCondition cnd = new SQLServerDAL.Search.SearchCondition();

            bool _val = true;
            if (fields != null)
                foreach (string _value in fieldsvalue)
                {
                    if (FilterHandler.ExistBadWord(_value))
                        _val = false;

                    if (!_val) break;
                }

            if (_val)
            {
                int i = 0;
                if (fields != null)
                    foreach (string _field in fields)
                    {
                        cnd.AddSearch(_field, fieldsvalue[i], MatchMode(matchmodes[i]));
                        i++;
                    }

                i = 0;
                if (sortFields != null)
                    foreach (string _field in sortFields)
                    {
                        cnd.AddSort(_field, sortMode[i] == "DESC" ? SortDir.desc : SortDir.asc);
                        i++;
                    }
            }
            else
                cnd.AddSearchSql(" AND 1 <> 1");

            SQLServerDAL.Search.CommonSearch _common = new SQLServerDAL.Search.CommonSearch("SQLMainDBConnString");

            return _common.ExecSelect(tableName, cnd);
        }
        /// <summary>
        /// object
        /// matchmodes(Equal, NotEqual, In, NotIn, Like, NotLike, MoreThan, MoreThanEqual, LessThan, LessThanEqual, StartWith, NotStartWith, Equal)
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="scalar"></param>
        /// <param name="fields"></param>
        /// <param name="matchmodes"></param>
        /// <param name="fieldsvalue"></param>
        /// <param name="sortFields"></param>
        /// <param name="sortMode"></param>
        /// <returns></returns>
        public static object ExecScalar(string tableName, string scalar, string[] fields, string[] matchmodes, string[] fieldsvalue, string[] sortFields, string[] sortMode)
        {
            SQLServerDAL.Search.SearchCondition cnd = new SQLServerDAL.Search.SearchCondition();
            bool _val = true;
            if (fields != null)
                foreach (string _value in fieldsvalue)
                {
                    if (FilterHandler.ExistBadWord(_value))
                        _val = false;

                    if (!_val) break;
                }

            if (_val)
            {
                int i = 0;
                if (fields != null)
                    foreach (string _field in fields)
                    {
                        cnd.AddSearch(_field, fieldsvalue[i], MatchMode(matchmodes[i]));
                        i++;
                    }

                i = 0;
                if (sortFields != null)
                    foreach (string _field in sortFields)
                    {
                        cnd.AddSort(_field, sortMode[i] == "DESC" ? SortDir.desc : SortDir.asc);
                        i++;
                    }
            }
            else
                cnd.AddSearchSql(" AND 1 <> 1");

            cnd.AddFields(scalar);

            SQLServerDAL.Search.CommonSearch _common = new SQLServerDAL.Search.CommonSearch("SQLMainDBConnString");

            return _common.ExecScalar(tableName, cnd);
        }

        /// <summary>
        /// DataTable
        /// matchmodes(Equal, NotEqual, In, NotIn, Like, NotLike, MoreThan, MoreThanEqual, LessThan, LessThanEqual, StartWith, NotStartWith, Equal)
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="fields"></param>
        /// <param name="matchmodes"></param>
        /// <param name="fieldsvalue"></param>
        /// <param name="sortFields"></param>
        /// <param name="sortMode"></param>
        /// <returns></returns>
        public static DataTable ExecSelect(string tableName, string[] tableColumes, string[] fields, string[] matchmodes, string[] fieldsvalue, string[] sortFields, string[] sortMode)
        {
            SQLServerDAL.Search.SearchCondition cnd = new SQLServerDAL.Search.SearchCondition();

            bool _val = true;
            if (fields != null)
                foreach (string _value in fieldsvalue)
                {
                    if (FilterHandler.ExistBadWord(_value))
                        _val = false;

                    if (!_val) break;
                }

            if (_val)
            {
                int i = 0;
                if (fields != null)
                    foreach (string _field in fields)
                    {
                        cnd.AddSearch(_field, fieldsvalue[i], MatchMode(matchmodes[i]));
                        i++;
                    }

                if (tableColumes != null)
                    foreach (string _field in tableColumes)
                    {
                        cnd.AddFields(_field);
                    }

                i = 0;
                if (sortFields != null)
                    foreach (string _field in sortFields)
                    {
                        cnd.AddSort(_field, sortMode[i] == "DESC" ? SortDir.desc : SortDir.asc);
                        i++;
                    }
            }
            else
                cnd.AddSearchSql(" AND 1 <> 1");

            SQLServerDAL.Search.CommonSearch _common = new SQLServerDAL.Search.CommonSearch("SQLMainDBConnString");

            return _common.ExecSelect(tableName, cnd);
        }

        private static Mode MatchMode(string mode)
        {
            if (mode == "Equal")
                return Mode.Equal; 
            else if (mode == "NotEqual")
                return Mode.NotEqual; 
            else if (mode == "In")
                return Mode.In;  
            else if (mode == "NotIn")
                return Mode.NotIn; 
            else if (mode == "Like")
                return Mode.Like;  
            else if (mode == "NotLike")
                return Mode.NotLike; 
            else if (mode == "MoreThan")
                return Mode.MoreThan; 
            else if (mode == "MoreThanEqual")
                return Mode.MoreThanEqual; 
            else if (mode == "LessThan")
                return Mode.LessThan; 
            else if (mode == "LessThanEqual")
                return Mode.LessThanEqual; 
            else if (mode == "StartWith")
                return Mode.StartWith; 
            else if (mode == "EndWith")
                return Mode.EndWith; 
            else if (mode == "NotStartWith")
                return Mode.EndWith; 
            else if (mode == "NotEndWith")
                return Mode.NotEndWith; 
            else
                return Mode.Equal;
        }
    }
}
